package com.dao.impl;

import com.dao.BaseDeo;
import com.dao.ModDeo;
import com.entity.composite.DeveloperInfo;
import com.entity.composite.ModDownloadInfo;
import com.entity.composite.ModInfo;
import com.entity.element_relation.Element;
import com.entity.short_reviews.ModShortReviews;
import com.entity.version_relation.Version;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wang_
 * @version 2024.3.4.1
 * @description
 * @date 2025/7/14
 */
public class ModDeoImpl extends BaseDeo implements ModDeo {
	/// 获取精选模组
	@Override
	public List<ModInfo> getSelectedMods() {
		linkConn();
		List<ModInfo> list = new ArrayList<>();
		//language=MySQL
		String sql = "select * from `mod` m inner join main_element me on m.main_element_id = me.id limit 3 offset 0";
		ResultSet rs = exeQuery(sql);
		if (rs == null) {
			return list;
		}
		try {
			list = getModList(rs);
		} catch (SQLException e) {
			LOG.error("获取精选模组异常！");
			LOG.error(e);
		}
		closeAll(rs);
		return list;
	}
	
	/// 获取模组的基本信息
	@Override
	public ModInfo getBasicInformation(int modId) {
		linkConn();
		ModInfo mod = new ModInfo();
		//language=MySQL
		String sql = "select * from `mod` m inner join main_element me on m.main_element_id = me.id where m.id=?";
		ResultSet rs = exeQuery(sql, modId);
		if (rs == null) {
			return mod;
		}
		try {
			mod = getModList(rs).get(0);
		} catch (SQLException e) {
			LOG.error("获取模组基本信息异常！modId={}", modId);
			LOG.error(e);
		}
		closeAll(rs);
		return mod;
	}
	
	/// 获取模组开发者
	@Override
	public List<DeveloperInfo> getDeveloper(int modId) {
		linkConn();
		List<DeveloperInfo> list = new ArrayList<>();
		//language=MySQL
		String sql = " select * from mod_developer_relation mdr inner join developers d on mdr.developer_id = d.id where mod_id=?;";
		ResultSet rs = exeQuery(sql, modId);
		if (rs == null) {
			return list;
		}
		try {
			while (rs.next()) {
				DeveloperInfo developers = new DeveloperInfo();
				developers.setCover(rs.getString("d.cover"));
				developers.setName(rs.getString("d.name"));
				developers.setDescription(rs.getString("d.description"));
				developers.setTeam(rs.getInt("d.team") != 0);
				developers.setId(rs.getInt("d.id"));
				developers.setRelation(rs.getString("mdr.relation"));
				list.add(developers);
			}
		} catch (SQLException e) {
			LOG.error("获取MOD开发者信息异常！modId={}", modId);
			LOG.error(e);
		}
		closeAll(rs);
		return list;
	}
	
	/// 获取模组元素
	@Override
	public List<Element> getElement(int modId) {
		linkConn();
		List<Element> list = new ArrayList<>();
		//language=MySQL
		String sql = "SELECT * FROM mod_element_relation mer inner join element e on mer.element_id = e.id where mer.mod_id = ?";
		ResultSet rs = exeQuery(sql, modId);
		if (rs == null) {
			return list;
		}
		try {
			while (rs.next()) {
				Element element = new Element();
				element.setId(rs.getInt("e.id"));
				element.setName(rs.getString("e.name"));
				list.add(element);
			}
		} catch (SQLException e) {
			LOG.error("获取MOD元素异常！modId={}", modId);
			LOG.error(e);
		}
		closeAll(rs);
		return list;
	}
	
	/// 获取模组版本
	@Override
	public List<Version> getVersion(int modId) {
		linkConn();
		List<Version> list = new ArrayList<>();
		//language=MySQL
		String sql = "SELECT * FROM version v inner join mod_version_relation mvr on v.id = mvr.version_id where mvr.mod_id = ?";
		ResultSet rs = exeQuery(sql, modId);
		if (rs == null) {
			return list;
		}
		try {
			while (rs.next()) {
				Version element = new Version();
				element.setId(rs.getInt("v.id"));
				element.setName(rs.getString("v.name"));
				list.add(element);
			}
		} catch (SQLException e) {
			LOG.error("获取模组版本异常！modId={}", modId);
			LOG.error(e);
		}
		closeAll(rs);
		return list;
	}
	
	/// 获取模组短评
	@Override
	public List<ModShortReviews> getShortReviews(int modId) {
		linkConn();
		List<ModShortReviews> list = new ArrayList<>();
		//language=MySQL
		String sql = "select * from mod_short_reviews where mod_id = ?";
		ResultSet rs = exeQuery(sql, modId);
		if (rs == null) {
			return list;
		}
		try {
			while (rs.next()) {
				ModShortReviews element = new ModShortReviews();
				element.setId(rs.getInt("id"));
				element.setFatherId(rs.getInt("father_id"));
				element.setPublisherId(rs.getInt("publisher_id"));
				element.setContent(rs.getString("content"));
				element.setLikeCount(rs.getInt("like_count"));
				element.setDate(rs.getString("date"));
				list.add(element);
			}
		} catch (SQLException e) {
			LOG.error("获取MOD短评信息异常！modId={}", modId);
			LOG.error(e);
		}
		closeAll(rs);
		return list;
	}
	
	/// 获取模组下载
	@Override
	public List<ModDownloadInfo> getDownload(int modId) {
		linkConn();
		List<ModDownloadInfo> list = new ArrayList<>();
		//language=MySQL
		String sql = "SELECT * FROM mod_download md inner join version v on md.version_id = v.id WHERE md.mod_id = ?";
		ResultSet rs = exeQuery(sql, modId);
		if (rs == null) {
			return list;
		}
		try {
			while (rs.next()) {
				ModDownloadInfo download = new ModDownloadInfo();
				download.setId(rs.getInt("md.id"));
				download.setModId(modId);
				download.setLink(rs.getString("md.link"));
				download.setName(rs.getString("md.name"));
				download.setVersionId(rs.getInt("md.version_id"));
				download.setVersionName(rs.getString("v.name"));
				list.add(download);
			}
		} catch (SQLException e) {
			LOG.error("获取MOD下载信息异常！modId={}", modId);
			LOG.error(e);
		}
		closeAll(rs);
		return list;
	}
	
	private List<ModInfo> getModList(ResultSet rs) throws SQLException {
		List<ModInfo> list = new ArrayList<>();
		while (rs.next()) {
			ModInfo mod = new ModInfo();
			int id = rs.getInt("m.id");
			mod.setId(id);
			mod.setName(rs.getString("m.name"));
			mod.setCover(rs.getString("m.cover"));
			mod.setIntroductionInfo(rs.getString("m.introduction_info"));
			mod.setTotalViews(rs.getInt("m.total_views"));
			mod.setFavoriteCount(rs.getInt("m.favorite_count"));
			mod.setLikeCount(rs.getInt("m.like_count"));
			mod.setDate(rs.getString("m.date"));
			mod.setMainElementId(rs.getInt("m.main_element_id"));
			mod.setReviewStatusId(rs.getInt("m.review_status_id"));
			mod.setMainElementName(rs.getString("me.name"));
			list.add(mod);
		}
		return list;
	}
}
